﻿// ParseLib - An open source library for natural language processing.
// Copyright (C) Kais Dukes, 2012. E-mail: kais@kaisdukes.com
// http://parselib.org

using System.Collections.Generic;
using ParseLib.Text;

namespace ParseLib.Corpora.Brown
{
    public class BrownReader : ForwardReader
    {
        private readonly List<Token> _tokens = new List<Token>();
        private readonly ICorpus _corpus;

        public BrownReader(ICorpus corpus)
        {
            _corpus = corpus;
        }

        public TextualUnit ReadSentence(string text)
        {
            _tokens.Clear();
            Text = text;

            // (<whitespace> <word> '/' <tag>)*
            while (!End)
            {
                ReadWhitespace();
                ReadToken();
            }

            return _tokens.Count != 0 ? new TextualUnit(_corpus, _tokens) : null;
        }

        private void ReadToken()
        {
            if (End) return;

            // <word>
            var index = _position;
            Read();
            while (Peek() != '/')
            {
                Read();
            }
            var word = _text.Substring(index, _position - index);

            // '/'
            Read();

            // <tag>
            index = _position;
            while (!End && !IsWhitespace(Peek()))
            {
                Read();
            }
            var tag = _text.Substring(index, _position - index);

            // Add token.
            _tokens.Add(new Token(word, _corpus.PartOfSpeechTagset.Build(tag)));
        }

        private void ReadWhitespace()
        {
            while (!End && IsWhitespace(Peek()))
            {
                Read();
            }
        }

        private static bool IsWhitespace(char ch)
        {
            return ch == ' ' || ch == '\t';
        }
    }
}